﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_PopedomManage_frm_UserPopedom : System.Web.UI.Page
{
    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindSysUserData(dst, intPageIndex);
        }
    }

    public void SetUserPopedom(object sender, ImageClickEventArgs e)
    {
        ImageButton ibtn = (ImageButton)sender;
        string strCommand = ibtn.CommandName;
        string strUserID = Request.Form["UserID"];

        if (strUserID != null && strUserID != "")
        {
            this.lblJs.Text = "<script>ModalDialog('frm_RolesAllot.aspx?UserID=" + strUserID + "&PopedomType=" + strCommand + "', 500, 400);</script>";
        }
        else
        {
            this.lblJs.Text = "<script>alert('请先选择一个操作员！');</script>";
        }
    }

    private void GetSysUserData(string UserID)
    {

        ClsUserLoginDAL cls_User = new ClsUserLoginDAL();

        DataSet dst = cls_User.GetSysUsers(UserID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetCorporationDataSet = dst;

            this.BindSysUserData(dst, 0);
        }
        else
        {
            this.dgrdResult.CurrentPageIndex = 0;
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
            this.lblRsCounts.Text = "共0条记录";
            this.lblPageCounts.Text = "共0页";
            this.lblCurrPage.Text = "当前第0页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = false;
        }
    }

    private void BindSysUserData(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = true;

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
                this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                this.GetSysUserData(strUserID);
            }
            else
            {
                this.lblJs.Text = "";
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    int intIndex = Int32.Parse(strIndex) - 1;

                    if (intIndex >= 0 && intIndex < this.dgrdResult.PageCount)
                    {
                        this.BindSysUserData(dst, intIndex);
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('请输入正确的页码数！')</script>";
                    }
                }
                catch
                {
                    this.lblJs.Text = "<script>alert('请输入数字类型的页码！')</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('请输入要跳转的页码！')</script>";
            }
        }
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            this.GetSysUserData(strUserID);
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            TextBox txt3 = (TextBox)e.Item.FindControl("txt3");
            TextBox txt4 = (TextBox)e.Item.FindControl("txt4");
            TextBox txt5 = (TextBox)e.Item.FindControl("txt5");
            TextBox txt6 = (TextBox)e.Item.FindControl("txt6");
            Label chkRsSelect = (Label)e.Item.FindControl("chkRsSelect");

            int intOver = Int32.Parse(e.Item.Cells[1].Text.Trim());
            bool is_stop = bool.Parse(e.Item.Cells[2].Text.Trim());
            int intUserType = Int32.Parse(e.Item.Cells[3].Text.Trim());

            chkRsSelect.Text = "<input type=radio name='UserID' value=" + e.Item.Cells[4].Text.Trim() + ">";
            txt4.Text = (!is_stop) ? "启用" : "停用";

            //密码有效期
            if (intOver == 0)
            {
                txt3.Text = "永远有效";
            }
            else if (intOver == 1)
            {
                txt3.Text = "1个月";
            }
            else if (intOver == 2)
            {
                txt3.Text = "3个月";
            }
            else
            {
                txt3.Text = "6个月";
            }

            //操作员性质
            if (intUserType == 1)
            {
                txt6.Text = "系统操作员";
            }
            else if (intUserType == 2)
            {
                txt6.Text = "公司领导";
            }
            else if (intUserType == 3)
            {
                txt6.Text = "部门经理";
            }
            else
            {
                txt6.Text = "普通员工";
            }

            //操作员岗位
            ClsStationDataDAL cls_UserStation = new ClsStationDataDAL();
            DataSet dst = cls_UserStation.GetStationDatas(1);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                string strID = e.Item.Cells[5].Text.Trim();
                int intItems = dst.Tables[0].Rows.Count - 1;

                for (int i = intItems; i >= 0; i--)
                {
                    if (dst.Tables[0].Rows[i][0].ToString().Trim() == strID)
                    {
                        txt5.Text = dst.Tables[0].Rows[i][1].ToString().Trim();
                        break;
                    }
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('获取岗位信息为空，请确保岗位信息已通过确认！')</script>";
            }
        }
    }
}
